package com.restart.game.common.base;

import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

public abstract class BaseServicesImpl<T, ID> implements BaseServices<T, ID> {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public abstract MyBaseMapper<T> getMapper();

    @Override
    @Transactional(rollbackFor = Exception.class) //事务回滚
    public Integer add(T t) {
        return getMapper().insertSelective(t); //封装单表操作方法
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteById(ID id) {
        return getMapper().deleteByPrimaryKey(id);
    }

    @Override
    public Integer batchAdd(List<T> list) {
        MyBaseMapper<T> mapper = getMapper();
        for (T t : list) {
            mapper.insertSelective(t);
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class) //事务回滚
    public Integer delete(T t) {
        return getMapper().delete(t);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) //事务回滚
    public Integer updateByPrimaryKey(T t) {
        return getMapper().updateByPrimaryKey(t);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) //事务回滚
    public Integer batchUpdateByPrimaryKey(List<T> list) {
        MyBaseMapper<T> mapper = getMapper();
        for (T t : list) {
            mapper.updateByPrimaryKey(t);
        }
        return 1;
    }

    @Override
    public T findOne(T t) {
        return getMapper().selectOne(t);
    }

    @Override
    public List<T> findAll() {
        return getMapper().selectAll();
    }

    @Override
    public T findById(ID id) {
        return getMapper().selectByPrimaryKey(id);
    }

    @Override
    public List<T> find(T t) {
        return getMapper().select(t);
    }

    @Override
    public List<T> findByExample(Example example) {
        return getMapper().selectByExample(example);
    }

    @Override
    public Integer findCountByExample(Example query) {
        List<T> ts = getMapper().selectByExample(query);
        return ts == null ? 0 : ts.size();
    }

    /**
     * TODO: 未实现 方法
     */
    @Override
    public Integer updateByExampleSelective(QueryExample<T> queryExample) {
        return null;
    }
    @Override
    public PageInfo<T> findPage(T t, Integer pageNum, Integer pageSize) {
        return null;
    }
    @Override
    public Integer updateByExampleSelective(T record, Example query) {
        return null;
    }
    @Override
    public Integer updateByExampleSelective(T record, T query) {
        return null;
    }
    @Override
    public Integer findCount(T record) {
        return null;
    }

}
